"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
/**
 * 前序遍历节点并使用 callback 进行转换
 */
const map = (treeNodes, callback, options) => {
  const {
    level = 0,
    childrenKey = 'children',
    parent
  } = options ?? {};
  return treeNodes.map((treeNode, index) => {
    const children = treeNode[childrenKey];
    if (children?.length) {
      const treeNodeWithOutChildren = {};
      Object.keys(treeNode).forEach(key => {
        // @ts-ignore
        if (key !== childrenKey) treeNodeWithOutChildren[key] = treeNode[key];
      });
      const convertedTreeNode = callback(treeNodeWithOutChildren, {
        level,
        index,
        parent,
        isMaybeLeaf: false
      });
      return {
        ...convertedTreeNode,
        [childrenKey]: map(children, callback, {
          childrenKey,
          level: level + 1,
          parent: treeNode
        })
      };
    }
    return callback(treeNode, {
      level,
      index,
      parent,
      isMaybeLeaf: true
    });
  });
};
var _default = exports.default = map;